package com.raisecom.base;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MyClassLoader extends ClassLoader {

	protected Class<?> findClass(String name) throws ClassNotFoundException {
		byte[] bytes = loadClassByte(name);
		
		Class<?> clazz = defineClass(name, bytes, 0, bytes.length);
		return clazz;
	}

	private byte[] loadClassByte(String name) {
		FileInputStream fis = null;
		ByteArrayOutputStream baos = null;
		
		try {
			fis = new FileInputStream(new File("E:/workspace3.7/SolutionMethod/bin/Caesar.class"));
			
			baos = new ByteArrayOutputStream();
			
			byte[] bufs = new byte[1024];
			int len = 0;
			
			while ((len = fis.read(bufs)) != -1) {
				baos.write(bufs, 0, len);
			}
			
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	
	public static void main(String[] args) throws ClassNotFoundException {
		MyClassLoader loader = new MyClassLoader();
		
		Class<?> clazz = loader.loadClass("Caesar");
		
		System.out.println(clazz.getSimpleName());
	}
	

}
